import axios from 'axios'
import { basicServerURL, contentServerURL } from './tools'
import nProgress from 'nprogress'
import 'nprogress/nprogress.css'
// 创建一个自定义的 Axios 实例
const basicInstance = axios.create({
  baseURL: basicServerURL,
  timeout: 5000
})
const contentInstance = axios.create({
  baseURL: contentServerURL,
  timeout: 5000
})
// 添加请求拦截器
basicInstance.interceptors.request.use(
  (config) => {
    // 启动loading
    nProgress.start()
    return config
  },
  (err) => {
    return Promise.reject(err)
  }
)
// 响应拦截器
basicInstance.interceptors.response.use(
  (res) => {
    // 关闭loading
    nProgress.done()
    return res
  },
  (err) => {
    nProgress.done()
    return Promise.reject(err)
  }
)
// 添加请求拦截器
contentInstance.interceptors.request.use(
  (config) => {
    // 启动loading
    nProgress.start()
    return config
  },
  (err) => {
    return Promise.reject(err)
  }
)
// 响应拦截器
contentInstance.interceptors.response.use(
  (res) => {
    // 关闭loading
    nProgress.done()
    return res
  },
  (err) => {
    nProgress.done()
    return Promise.reject(err)
  }
)

export const basicGet = (url, params) => {
  return basicInstance.get(url, { params }).then((res) => res.data)
}
// post请求封装
export const basicPost = (url, data) => {
  return basicInstance.post(url, data).then((res) => res.data)
}
// put请求封装
export const basicPut = (url, data) => {
  return basicInstance.put(url, data).then((res) => res.data)
}
// delete请求封装
export const basicDelete = (url, params) => {
  return basicInstance.delete(url, { params }).then((res) => res.data)
}

export const contentGet = (url, params) => {
  return contentInstance.get(url, { params }).then((res) => res.data)
}
// post请求封装
export const contentPost = (url, data) => {
  return contentInstance.post(url, data).then((res) => res.data)
}
// put请求封装
export const contentPut = (url, data) => {
  return contentInstance.put(url, data).then((res) => res.data)
}
// delete请求封装
export const contentDelete = (url, params) => {
  return contentInstance.delete(url, { params }).then((res) => res.data)
}
